All files / core/view/filter RangedFilter.ts

95.92% Statements 47/49
91.67% Branches 11/12
92.86% Functions 13/14
95.83% Lines 46/48
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207                                12x 12x 12x 12x                           12x                                                                 144x 144x 144x 144x           12x 1114x           12x 1089x             12x 761x                 12x               29x 7x   29x                         12x         158x       158x   158x 158x 158x 315x 27x     158x                   12x               12x 201x           12x 549x           12x 248x               12x 144x 59x 59x   85x                 12x 144x 47x 47x   97x     12x  
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { IndexedFilter } from './IndexedFilter';
import { PRIORITY_INDEX } from '../../snap/indexes/PriorityIndex';
import { NamedNode, Node } from '../../../core/snap/Node';
import { ChildrenNode } from '../../snap/ChildrenNode';
import { NodeFilter } from './NodeFilter';
import { QueryParams } from '../QueryParams';
import { Index } from '../../snap/indexes/Index';
import { Path } from '../../util/Path';
import { CompleteChildSource } from '../CompleteChildSource';
import { ChildChangeAccumulator } from '../ChildChangeAccumulator';
 
/**
 * Filters nodes by range and uses an IndexFilter to track any changes after filtering the node
 *
 * @constructor
 * @implements {NodeFilter}
 */
export class RangedFilter implements NodeFilter {
  /**
   * @type {!IndexedFilter}
   * @const
   * @private
   */
  private indexedFilter_: IndexedFilter;
 
  /**
   * @const
   * @type {!Index}
   * @private
   */
  private index_: Index;
 
  /**
   * @const
   * @type {!NamedNode}
   * @private
   */
  private startPost_: NamedNode;
 
  /**
   * @const
   * @type {!NamedNode}
   * @private
   */
  private endPost_: NamedNode;
 
  /**
   * @param {!QueryParams} params
   */
  constructor(params: QueryParams) {
    this.indexedFilter_ = new IndexedFilter(params.getIndex());
    this.index_ = params.getIndex();
    this.startPost_ = RangedFilter.getStartPost_(params);
    this.endPost_ = RangedFilter.getEndPost_(params);
  }
 
  /**
   * @return {!NamedNode}
   */
  getStartPost(): NamedNode {
    return this.startPost_;
  }
 
  /**
   * @return {!NamedNode}
   */
  getEndPost(): NamedNode {
    return this.endPost_;
  }
 
  /**
   * @param {!NamedNode} node
   * @return {boolean}
   */
  matches(node: NamedNode): boolean {
    return (
      this.index_.compare(this.getStartPost(), node) <= 0 &&
      this.index_.compare(node, this.getEndPost()) <= 0
    );
  }
 
  /**
   * @inheritDoc
   */
  updateChild(
    snap: Node,
    key: string,
    newChild: Node,
    affectedPath: Path,
    source: CompleteChildSource,
    optChangeAccumulator: ChildChangeAccumulator | null
  ): Node {
    if (!this.matches(new NamedNode(key, newChild))) {
      newChild = ChildrenNode.EMPTY_NODE;
    }
    return this.indexedFilter_.updateChild(
      snap,
      key,
      newChild,
      affectedPath,
      source,
      optChangeAccumulator
    );
  }
 
  /**
   * @inheritDoc
   */
  updateFullNode(
    oldSnap: Node,
    newSnap: Node,
    optChangeAccumulator: ChildChangeAccumulator | null
  ): Node {
    Iif (newSnap.isLeafNode()) {
      // Make sure we have a children node with the correct index, not a leaf node;
      newSnap = ChildrenNode.EMPTY_NODE;
    }
    let filtered = newSnap.withIndex(this.index_);
    // Don't support priorities on queries
    filtered = filtered.updatePriority(ChildrenNode.EMPTY_NODE);
    const self = this;
    newSnap.forEachChild(PRIORITY_INDEX, function(key, childNode) {
      if (!self.matches(new NamedNode(key, childNode))) {
        filtered = filtered.updateImmediateChild(key, ChildrenNode.EMPTY_NODE);
      }
    });
    return this.indexedFilter_.updateFullNode(
      oldSnap,
      filtered,
      optChangeAccumulator
    );
  }
 
  /**
   * @inheritDoc
   */
  updatePriority(oldSnap: Node, newPriority: Node): Node {
    // Don't support priorities on queries
    return oldSnap;
  }
 
  /**
   * @inheritDoc
   */
  filtersNodes(): boolean {
    return true;
  }
 
  /**
   * @inheritDoc
   */
  getIndexedFilter(): IndexedFilter {
    return this.indexedFilter_;
  }
 
  /**
   * @inheritDoc
   */
  getIndex(): Index {
    return this.index_;
  }
 
  /**
   * @param {!QueryParams} params
   * @return {!NamedNode}
   * @private
   */
  private static getStartPost_(params: QueryParams): NamedNode {
    if (params.hasStart()) {
      const startName = params.getIndexStartName();
      return params.getIndex().makePost(params.getIndexStartValue(), startName);
    } else {
      return params.getIndex().minPost();
    }
  }
 
  /**
   * @param {!QueryParams} params
   * @return {!NamedNode}
   * @private
   */
  private static getEndPost_(params: QueryParams): NamedNode {
    if (params.hasEnd()) {
      const endName = params.getIndexEndName();
      return params.getIndex().makePost(params.getIndexEndValue(), endName);
    } else {
      return params.getIndex().maxPost();
    }
  }
}